﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports SdlDotNet.Input
Imports System.Drawing

Public Class DeployDefenseMenu
    Implements IMenu

    Private Position As Point

    Private AntiAircraftAvailableSurface As New Surface("Resources\Images\Game\Menu\AntiAircraft_Available.jpg")
    Private AntiAircraftUnavailableSurface As New Surface("Resources\Images\Game\Menu\AntiAircraft_Unavailable.jpg")
    Private AntiAircraftSelectedSurface As New Surface("Resources\Images\Game\Menu\AntiAircraft_Selected.jpg")
    Private AntiMissileAvailableSurface As New Surface("Resources\Images\Game\Menu\AntiMissile_Available.jpg")
    Private AntiMissileUnavailableSurface As New Surface("Resources\Images\Game\Menu\AntiMissile_Unavailable.jpg")
    Private AntiMissileSelectedSurface As New Surface("Resources\Images\Game\Menu\AntiMissile_Selected.jpg")
    Private AntiMissileDeployedSurface As New Surface("Resources\Images\Game\Menu\AntiMissileDeployed_Unavailable.jpg")
    Private AntiAircraftDeployedSurface As New Surface("Resources\Images\Game\Menu\AntiAircraftDeployed_Unavailable.jpg")
    Private CancelAvailableSurface As New Surface("Resources\Images\Game\Menu\Cancel_Available.jpg")
    Private CancelSelectedSurface As New Surface("Resources\Images\Game\Menu\Cancel_Selected.jpg")
    Private BlankSurface1 As New Surface("Resources\Images\Game\Menu\Blank_3.jpg")
    Private BlankSurface2 As New Surface("Resources\Images\Game\Menu\Blank_4.jpg")
    Private BlankSurface3 As New Surface("Resources\Images\Game\Menu\Blank_5.jpg")

    Private AntiAircraftButton As Sprite
    Private AntiMissileButton As Sprite
    Private BlankButton1 As Sprite
    Private BlankButton2 As Sprite
    Private BlankButton3 As Sprite
    Private CancelButton As Sprite

    Private CanSelectAntiAircraft As Boolean = False
    Private CanSelectAntiMissile As Boolean = False
    Private AntiAircraftDeployed As Boolean = False
    Private AntiMissileDeployed As Boolean = False

    Public Event ClickedAntiAircraftButton()
    Public Event ClickedAntiMissileButton()
    Public Event ClickedCancelButton()

    Public Sub New(ByVal position As Point, ByVal player As Player)
        Me.Position = position
        InitializeSprites()
        ShowMenuForPlayer(player)
    End Sub

    Private Sub InitializeSprites()
        AntiAircraftButton = New Sprite(AntiAircraftUnavailableSurface, New Point(Position.X, Position.Y))
        AntiMissileButton = New Sprite(AntiMissileUnavailableSurface, New Point(Position.X, Position.Y + 19))
        BlankButton1 = New Sprite(BlankSurface1, New Point(Position.X, Position.Y + (19 * 2)))
        BlankButton2 = New Sprite(BlankSurface2, New Point(Position.X, Position.Y + (19 * 3)))
        BlankButton3 = New Sprite(BlankSurface3, New Point(Position.X, Position.Y + (19 * 4)))
        CancelButton = New Sprite(CancelAvailableSurface, New Point(Position.X, Position.Y + (19 * 5)))
    End Sub

    Public Sub Draw(ByRef display As Surface) Implements IMenu.Draw
        display.Blit(AntiAircraftButton)
        display.Blit(AntiMissileButton)
        display.Blit(BlankButton1)
        display.Blit(BlankButton2)
        display.Blit(BlankButton3)
        display.Blit(CancelButton)
    End Sub

    Public Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Implements IMenu.HandleMouseButtonUp
        If (AntiAircraftButton.IntersectsWith(e.Position) And CanSelectAntiAircraft) Then
            RaiseEvent ClickedAntiAircraftButton()
        ElseIf (AntiMissileButton.IntersectsWith(e.Position) And CanSelectAntiMissile) Then
            RaiseEvent ClickedAntiMissileButton()
        ElseIf (CancelButton.IntersectsWith(e.Position)) Then
            RaiseEvent ClickedCancelButton()
        End If
    End Sub

    Public Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs) Implements IMenu.HandleMouseMotion
        ApplyPlayerView()

        If (AntiAircraftButton.IntersectsWith(e.Position) And CanSelectAntiAircraft) Then
            AntiAircraftButton.Surface = AntiAircraftSelectedSurface
        ElseIf (AntiMissileButton.IntersectsWith(e.Position) And CanSelectAntiMissile) Then
            AntiMissileButton.Surface = AntiMissileSelectedSurface
        ElseIf (CancelButton.IntersectsWith(e.Position)) Then
            CancelButton.Surface = CancelSelectedSurface
        End If
    End Sub

    Public Sub ShowMenuForPlayer(ByRef player As Player) Implements IMenu.ShowMenuForPlayer
        If (player.Inventory.AntiAircraft > 0 And player.AntiAircraftDeployed = False) Then
            CanSelectAntiAircraft = True
        Else
            CanSelectAntiAircraft = False
        End If
        If (player.Inventory.AntiMissile > 0 And player.AntiMissileDeployed = False) Then
            CanSelectAntiMissile = True
        Else
            CanSelectAntiMissile = False
        End If

        AntiAircraftDeployed = player.AntiAircraftDeployed()
        AntiMissileDeployed = player.AntiMissileDeployed()

        HandleMouseMotion(Me, New MouseMotionEventArgs(False, MouseButton.None, CShort(Mouse.MousePosition.X), CShort(Mouse.MousePosition.Y), CShort(Mouse.MousePositionChange.X), CShort(Mouse.MousePositionChange.Y)))
    End Sub

    Private Sub ApplyPlayerView()
        CancelButton.Surface = CancelAvailableSurface

        If (CanSelectAntiAircraft) Then
            AntiAircraftButton.Surface = AntiAircraftAvailableSurface
        ElseIf (AntiAircraftDeployed) Then
            AntiAircraftButton.Surface = AntiAircraftDeployedSurface
        Else
            AntiAircraftButton.Surface = AntiAircraftUnavailableSurface
        End If

        If (CanSelectAntiMissile) Then
            AntiMissileButton.Surface = AntiMissileAvailableSurface
        ElseIf (AntiMissileDeployed) Then
            AntiMissileButton.Surface = AntiMissileDeployedSurface
        Else
            AntiMissileButton.Surface = AntiMissileUnavailableSurface
        End If
    End Sub

    Public Sub Dispose() Implements IMenu.Dispose
        AntiMissileAvailableSurface.Dispose()
        AntiMissileUnavailableSurface.Dispose()
        AntiMissileSelectedSurface.Dispose()
        AntiAircraftAvailableSurface.Dispose()
        AntiAircraftUnavailableSurface.Dispose()
        AntiAircraftSelectedSurface.Dispose()
        CancelAvailableSurface.Dispose()
        CancelSelectedSurface.Dispose()
        BlankSurface1.Dispose()
        BlankSurface2.Dispose()
        BlankSurface3.Dispose()
        AntiAircraftDeployedSurface.Dispose()
        AntiMissileDeployedSurface.Dispose()
    End Sub
End Class